The Java Virtual Machine (JVM) runs a Java program by executing “bytecodes.” Each bytecode is an instruction, usually one byte in size, which tells the JVM to perform a specific operation. Java bytecode language is a form of assembly language.
Among the bytecodes recognized by the JVM is a particular bytecode called “invokevirtual.” The “invokevirtual” bytecode represents the invocation of a virtual method in corresponding Java source code (a virtual method is a method that can be overridden in a subclass). The “invokevirtual” bytecode is accompanied by an operand that identifies both a class and a method of that class; the operand identifies the method that is to be invoked. In practice, the operand is a 16-bit index into a table that is called the “constant pool;” this table contains information about the method.
Before the “invokevirtual” bytecode is executed for a particular method, one or more operands need to be pushed onto the operand stack. For example, a reference to the object relative to which the particular method is to be invoked (the “receiver”) needs to be pushed onto the operand stack. Additionally, if the particular method requires any parameters, then the actual parameters that are being passed to the invocation of the particular method need to be pushed onto the operand stack also. These operands are pushed onto the operand stack via the “push” bytecode.
Unlike some other assembly languages, the Java bytecode language is typed. A mechanism of the JVM called the “verifier” examines the bytecodes before execution and tracks the types of the operands that are going to be pushed onto the operand stack. Before the program containing the bytecode is executed, the verifier determines whether the types of the operands that will be on the stack at the time that the “invokevirtual” bytecode is executed will match the types of the formal parameters that correspond to those operands (the types of the formal parameters are indicated in the declaration of the method that is to be invoked). If the verifier detects any mismatch, then the verifier may indicate the presence of an error.
For example, if the declaration of a method “C.foo( )” specifies two formal parameters, “bar” and “baz,” and if “bar” is declared to be type “Integer” while “baz” is declared to be type “List,” then the verifier will determine whether, at the time that the “invokevirtual” bytecode for method “C.foo( )” will be executed, the operands on the operand stack will be of types “Integer” and “List,” respectively.
The type checking performed by the verifier helps to ensure that the Java runtime environment is safe and robust. Among other assurances, preventing type mismatches helps to ensure that bytecodes are not used for purposes for which they were not intended, so that, for example, a Java program cannot perform operations that will corrupt memory areas that have not been reserved to that program. When bytecode is downloaded from the Internet, the bytecode may be passed through the verifier prior to execution to make sure that the bytecode will not perform any forbidden operations when executed. After the bytecode has been verified once, there is no need to type check the bytecode every time the bytecode is executed. Due to this fact, bytecode may be executed more rapidly.
Generally speaking, Java is a statically typed language. In Java source code, a type usually needs to be declared expressly for each object. However, not every programming language is statically typed in this manner. For example, the Perl and Python programming languages do not require type declarations; rather than being statically typed, these languages are dynamically typed. Despite these latter languages being dynamically typed, it would be beneficial if bytecode that was generated based on programs written in these languages could be executed by the JVM.
However, because the verifier currently requires the actual arguments that are on the operand stack to match the declared types of the formal parameters of an invoked method when the “invokevirtual” bytecode is executed, bytecode representations of programs that are written in dynamically typed languages will always cause the verifier to issue an error and prevent execution. There are typically no declared types for formal parameters of methods in such programs, so the verifier has no way of checking the types of the arguments that are on the operand stack.